﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using QT.Windows.Shell;

namespace LogAnalytics
{
    public partial class Form1 : GlassForm
    {
        #region Attributes

        private Thread myThread = null;

        #endregion 

        #region Form

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            loadForm();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.menuStrip1.BackColor = AeroColor;
            this.statusStrip1.BackColor = AeroColor;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            e.Cancel = true;
            if (myThread != null && myThread.IsAlive)
            {
                if (MessageBox.Show("正在进行数据查询，确认要关闭吗？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel) return;
                myThread.Abort();
            }
            e.Cancel = false;
        }

        #endregion

        #region Event

        #region GlassButton

        private void gbtnAnaly_Click(object sender, EventArgs e)
        {
            toolStripMenuItem_Analytics_Click(sender, e);
        }

        private void gbtnCheck_Click(object sender, EventArgs e)
        {
            if (this.cbbSiteList.SelectedValue.ToString().Length == 0)
            {
                MessageBox.Show("请选择站点");
                return;
            }
            Regex regex = new Regex(@"^\d+$");
            if (!regex.IsMatch(this.tbTop.Text))
            {
                MessageBox.Show("记录数输入必须为正整数或0，0代表查询所有记录");
                this.tbTop.Focus();
                return;
            }
            if (this.rbtnGeneral.Checked && this.dtpStart.Value > this.dtpEnd.Value)
            {
                MessageBox.Show("开始日期必须小于结束日期");
                return;
            }

            DataTable dt = null;
            try
            {
                if (this.rbtnGeneral.Checked) dt = LogBase.Log.getAnalybyGeneral(this.dtpStart.Value.ToString("yyyy-MM-dd"), this.dtpEnd.Value.ToString("yyyy-MM-dd"), this.cbbSiteList.SelectedValue.ToString());
                if (this.rbtnPage.Checked) dt = LogBase.Log.getAnalybyPage(this.dtpStart.Value.ToString("yyyy-MM-dd"), this.cbbSiteList.SelectedValue.ToString(), this.rbtnTel.Checked ? "1" : "0", int.Parse(this.tbTop.Text));
                if (this.rbtnIP.Checked) dt = LogBase.Log.getAnalybyIP(this.dtpStart.Value.ToString("yyyy-MM-dd"), this.cbbSiteList.SelectedValue.ToString(), this.rbtnTel.Checked ? "1" : "0", int.Parse(this.tbTop.Text));

                this.dgvResult.DataSource = dt;
                this.dgvResult.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region menuStrip

        #region File

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Edit

        private void toolStripMenuItem_Analytics_Click(object sender, EventArgs e)
        {
            Edit.Analytics myAnalytics = new Edit.Analytics();
            myAnalytics.ShowDialog();
            if (myAnalytics != null) myAnalytics.Dispose();
        }

        private void toolStripMenuItem_SiteManage_Click(object sender, EventArgs e)
        {
            Edit.SiteManage mySiteManage = new Edit.SiteManage();
            mySiteManage.ShowDialog();
            if (mySiteManage != null) mySiteManage.Dispose();
        }

        private void toolStripMenuItem_PageComment_Click(object sender, EventArgs e)
        {
            Edit.PageComment myComment = new Edit.PageComment();
            myComment.ShowDialog();
            if (myComment != null) myComment.Dispose();
        }

        private void toolStripMenuItem_PageFilter_Click(object sender, EventArgs e)
        {
            Edit.PageFilter myFilter = new Edit.PageFilter();
            myFilter.ShowDialog();
            if (myFilter != null) myFilter.Dispose();
        }

        #endregion

        #region Tools

        private void toolStripMenuItem_DataManage_Click(object sender, EventArgs e)
        {
            Tools.DataManager myManager = new Tools.DataManager();
            myManager.ShowDialog();
            if (myManager != null) myManager.Dispose();
        }

        private void toolStripMenuItem_Option_Click(object sender, EventArgs e)
        {
            Tools.Option myOption = new Tools.Option();
            myOption.ShowDialog();
            if (myOption != null) myOption.Dispose();
        }

        #endregion

        #region Help

        private void toolStripMenuItem_About_Click(object sender, EventArgs e)
        {
            MessageBox.Show("IISW3C日志分析工具 V2.0", "安全组", MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        #endregion

        #endregion

        #region RadioButton

        private void rbtnGeneral_CheckedChanged(object sender, EventArgs e)
        {
            bool b = this.rbtnGeneral.Checked;
            this.lblLine.Visible = b;
            this.dtpEnd.Visible = b;
        }

        #endregion

        #region DataGridView

        private void dgvResult_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("当前站点：{1}，共显示{0}条记录", this.dgvResult.RowCount, this.cbbSiteList.GetItemText(this.cbbSiteList.SelectedItem));
        }

        private void dgvResult_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            this.tsslPosition.Text = string.Format("      行 {0}      列 {1}", e.RowIndex + 1, e.ColumnIndex + 1);
        }

        #endregion

        #endregion

        #region Method

        private void loadForm()
        {
            DataTable dt = null;
            try
            {
                dt = LogBase.SiteManage.getUsing();
                this.cbbSiteList.DisplayMember = "sitename";
                this.cbbSiteList.ValueMember = "guid";
                this.cbbSiteList.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            this.dtpStart.Value = DateTime.Now.AddDays(-1);
        }

        #endregion
    }

    [SuppressUnmanagedCodeSecurity]
    internal static class SafeNativeMethods
    {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

        [DllImport("user32.dll", EntryPoint = "FindWindowA", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true)]
        public static extern IntPtr FindWindowA(string lp1, string lp2);

        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr ShowWindow(IntPtr hWnd, int _value);
    }
}
